# Part of the client dedicated to database connection.
# Author: Daniel Rivera
import sqlite3

#constants
DATABASENAME = 'sqliteSample.db'

class ModelData:
    
    _tables = {}
    _connection = None
    
    def __init__(self):
        self._connection = sqlite3.connect(DATABASENAME)
    
    def createTable(self,tablename, schema):
        #schema includes an array with a definition of columns in the table
        cursor = self._connection.cursor()
        aux = ''
        i = 0
        for n in schema:
            aux += n
            if i < len(schema) - 1:
                aux += ', '
            i += 1
        sql = '''create table ''' + tablename + '''(''' + aux + ''')'''
        print sql
        cursor.execute(sql)
        self._connection.commit()
        cursor.close()
        
    def insertData(self,tablename, attributes):
        #attributes store values to be inserted in the table indicated by tablename
        cursor = self._connection.cursor()
        sql = 'insert into ' + tablename + ' values ('
        i = 0
        while (i < len(attributes) - 1):
            sql += '?, '
        sql += '?)'
        cursor.execute(sql, attributes)
        self._connection.commit()
        cursor.close()
    
    
    